/**
 *模板管理
 */
var pageCurr;
var form;
$(function() {
    layui.use('table', function(){
        var table = layui.table;
        form = layui.form;

        tableIns=table.render({
            id:'id',
            elem: '#tempList',
            url:'/temp/tempList',
            method: 'post', //默认：get请求
            cellMinWidth: 80,
            page: true,
            height: 'full-'+240,
            request: {
                pageName: 'pageNum', //页码的参数名称，默认：pageNum
                limitName: 'pageSize' //每页数据量的参数名，默认：pageSize
            },
            response:{
                statusName: 'code', //数据状态的字段名称，默认：code
                statusCode: 200, //成功的状态码，默认：0
                countName: 'totals', //数据总数的字段名称，默认：count
                dataName: 'list' //数据列表的字段名称，默认：data
            },
            cols: [[
                 {field:'sort',title:'排序',align:'center',width:'5%'}
                ,{field:'tempId',title:'模板号',align:'center',width:'6%'}
                ,{field:'content',hide:'true'}
                ,{field:'tempTitle', title:'模板标题',align:'center'/*,width:"10%"*/}
                ,{field:'versionNo', title:'版本号',align:'center'/*,width:"10%"*/}

                ,{fixed:'right',title:'操作',align:'center', toolbar:'#optBar',width:"15%"}
            ]],
            done: function(res, curr, count){

            }
        });


        //监听工具条
        table.on('tool(tempTable)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                //删除
                del(data);
            }else if(obj.event ==='view'){
                view(data)
            } else if(obj.event === 'edit'){
                //编辑
                add(data);
            }
        });


    });

});

//跳转到新增页面
function add(data) {
    if (data == null){
        window.location.href= "/temp/addTempPage";
    } else {
        var form = $("<form method='post'></form>");
        var input;
        form.attr({"action":"/temp/addTempPage"});
        $.each(data,function (key,value) {
            input = $("<input type='hidden'>");
            input.attr({"name":key});
            input.val(value);
            form.append(input);
        });
        $(document.body).append(form);
        form.submit();

    }

}
//删除
function del(obj) {
        layer.confirm('您确定要删除吗？', {
            btn: ['确认','返回'] //按钮
        }, function(){
            $.post("/temp/del",{"tempId":obj.tempId},function(data){
                if (data.code == 1) {
                    layer.alert(data.msg,function(){
                        layer.closeAll();
                        load(obj);
                    });
                } else {
                    layer.alert(data.msg);
                }
            });
        }, function(){
            layer.closeAll();
        });

}
//重新加载table
function load(obj){
    tableIns.reload({
        where: obj.field
        , page: {
            curr: pageCurr //从当前页码开始
        }
    });
}
function view(data) {
        var form = $("<form method='post'></form>");
        var input;
        form.attr({"action":"/temp/tempDetail"});

        $.each(data,function (key,value) {
            input = $("<input type='hidden'>");
            input.attr({"name":key});
            input.val(value);
            form.append(input);
        });
        $(document.body).append(form);
        form.submit();
}